<template>
  <!-- #ifdef MP-WEIXIN -->
  <uv-navbar
    :fixed="false"
    :title="title"
    left-arrow
    @leftClick="$onClickLeft"
  />
  <!-- #endif -->
  <view class="p-3">
    <view class="bg-white p-1">
      <view class="flex content-center">
        <image class="headimg" :src="shop.image" mode="aspectFill"></image>
        <view class="title" style="line-height: 62rpx">
          {{ shop.name }}
        </view>
      </view>
      <view class="yuer">
        <image
          class="bgcimg"
          src="/static/images/merchant/indexbg.svg"
          mode=""
        ></image>
        <view class="one">
          {{ currentLanguageTexts.MyBalance }}
        </view>
        <view class="two">
          {{ shop.balance }}
        </view>
        <view
          class="three flex content-center"
          @click="gopage('/pages/components/pages/merchant/wallet')"
        >
          <span>{{ currentLanguageTexts.BalanceDetails }}</span>
          <uv-icon name="arrow-right" size="26rpx" color="#ffffff"></uv-icon>
        </view>
        <view
          class="four"
          @click="gopage('/pages/components/pages/merchant/cash')"
        >
          {{ currentLanguageTexts.Withdrawal }}
        </view>
        <view class="yuer_bom flex justify-between">
          <view class="flex content-center" v-if="isShow">
            <image src="/static/images/merchant/on.svg" mode=""></image>
            <text>{{ currentLanguageTexts.shopOpen }}</text>
          </view>
          <view class="flex content-center" v-else>
            <image src="/static/images/merchant/off.svg" mode=""></image>
            <text>{{ currentLanguageTexts.shopClose }}</text>
          </view>
          <uv-switch v-model="isShow" size="20" @change="change"></uv-switch>
        </view>
      </view>
    </view>
    <!-- 分类 -->
    <view class="card flex flex-wrap content-between">
      <view
        class="card_item"
        v-for="(item, i) in orderCountList"
        :key="i"
        @click="goOrder(i)"
      >
        <view class="type">
          <uv-badge type="error" max="99" :value="item.type"></uv-badge>
        </view>
        <image :src="item.url" mode=""></image>
        <view class="">
          {{ item.name }}
        </view>
      </view>
    </view>
    <!-- 今日数据 -->
    <view class="day_card">
      <view class="title flex content-center justify-between">
        <view class="flex content-center">
          <view class="one">
            {{ currentLanguageTexts.Todaydata }}
          </view>
        </view>
        <view class="flex content-center">
          <view
            class="two"
            style="line-height: 50rpx; margin-left: 20rpx; width: 370rpx"
          >
            {{ timer }} {{ currentLanguageTexts.RealUpdate }}
          </view>
          <view class="dayimg">
            <image
              style="margin-top: 10rpx"
              src="/static/images/merchant/refresh.svg"
              mode=""
              @click="getloding"
            ></image>
          </view>
        </view>
      </view>
      <!-- 数据 -->
      <view>
        <uv-grid :border="true" col="2">
          <uv-grid-item v-for="(item, Index) in gridlist" :key="Index">
            <view class="grid">
              <view class="grid_one">
                {{ item.title }}
              </view>
              <view class="grid_two">
                {{ item.type }}
              </view>
            </view>
          </uv-grid-item>
        </uv-grid>
      </view>
    </view>
    <uv-tabbar
      :value="value"
      @change="(index) => (value = index)"
      activeColor="#09b4f1"
    >
      <uv-tabbar-item
        :text="currentLanguageTexts.homePage"
        icon="home"
      ></uv-tabbar-item>
      <uv-tabbar-item
        :text="currentLanguageTexts.menuOrder"
        icon="order"
        @click="gopage('/pages/components/pages/merchant/order')"
      ></uv-tabbar-item>
      <uv-tabbar-item
        :text="currentLanguageTexts.Desk"
        icon="bell"
        @click="gopage('/pages/components/pages/merchant/bell')"
      ></uv-tabbar-item>
      <uv-tabbar-item
        :text="currentLanguageTexts.mine"
        icon="account"
        @click="gopage('/pages/components/pages/merchant/my')"
      ></uv-tabbar-item>
      <uv-tabbar-item
        :text="currentLanguageTexts.return"
        icon="arrow-leftward"
        @click="gopage2('/pages/index/index')"
      ></uv-tabbar-item>
    </uv-tabbar>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import { onShow, onUnload, onLoad, onHide } from '@dcloudio/uni-app';
import { getData, changeStatus } from '@/api/merchant';
import { useMainStore } from '@/store/store';
import { storeToRefs } from 'pinia';
import { formatDateTime } from '@/utils/util';
const main = useMainStore();
const { currentLanguageTexts } = storeToRefs(main);

const title = ref(currentLanguageTexts.value.MerchantCenter);
const isShow = ref(true);
const gridlist = ref([]);
const orderCountList = ref([]);
const shop = ref({});
const value = ref(0);
let timer = ref('');
let inter = null;

onLoad(() => {
  getHomeData();
  inter = setInterval(() => {
    getHomeData();
  }, 30000);
});

onUnload(() => {
  clearInterval(inter);
});

onHide(() => {
  clearInterval(inter);
});

const getTime = () => {
  let date = new Date();
  let hour = date.getHours();

  let endHour = new Date(shop.value.endTime).getHours();
  let startHour = new Date(shop.value.startTime).getHours();

  if (shop.value.status == 1) {
    if (hour >= startHour && hour <= endHour) {
      isShow.value = true;
    } else {
      isShow.value = false;
    }
  } else {
    isShow.value = false;
  }
};

const goOrder = (i) => {
  uni.navigateTo({
    url: '/pages/components/pages/merchant/order?index=' + (i + 1),
  });
};
const change = async () => {
  let status = 0;
  !isShow.value;
  if (isShow.value) {
    status = 1;
  }
  let res = await changeStatus({
    shopId: shop.value.id,
    status: status,
  });
};

const gopage = (url) => {
  uni.navigateTo({
    url,
  });
};

const gopage2 = (url) => {
  uni.switchTab({
    url,
  });
};

const getloding = async () => {
  uni.showLoading({
    title: currentLanguageTexts.value.loading,
  });
  getHomeData();
  uni.hideLoading();
};

const getHomeData = async () => {
  let res = await getData();
  shop.value = res.appStoreShopVO || {};
  main.SET_MERCHART_SHOP(res.appStoreShopVO);
  timer.value = formatDateTime(new Date());
  if (shop.value.status == 1) {
    isShow.value = true;
  } else {
    isShow.value = false;
  }
  getTime();
  orderCountList.value = [
    {
      url: '/static/images/merchant/order01.svg',
      name: currentLanguageTexts.value.Pendingorders,
      type: res.appOrderCountVO.num01,
    },
    {
      url: '/static/images/merchant/order02.svg',
      name: currentLanguageTexts.value.Pendinggoods,
      type: res.appOrderCountVO.num02,
    },
    {
      url: '/static/images/merchant/order03.svg',
      name: currentLanguageTexts.value.fourthStatusCompleted,
      type: res.appOrderCountVO.num03,
    },
    {
      url: '/static/images/merchant/order04.svg',
      name: currentLanguageTexts.value.Pendingrefund,
      type: res.appOrderCountVO.num04,
    },
    {
      url: '/static/images/merchant/order05.svg',
      name: currentLanguageTexts.value.Pendingpayment,
      type: res.appOrderCountVO.num05,
    },
    {
      url: '/static/images/merchant/order06.svg',
      name: currentLanguageTexts.value.Refunded,
      type: res.appOrderCountVO.num06,
    },
    {
      url: '/static/images/merchant/order07.svg',
      name: currentLanguageTexts.value.appointmentProgress,
      type: res.appOrderCountVO.num07,
    },
  ];
  gridlist.value = [
    {
      title: currentLanguageTexts.value.Todayrevenue,
      type: res.appTodayDataVO.num01,
    },
    {
      title: currentLanguageTexts.value.Todayorder,
      type: res.appTodayDataVO.num02,
    },
    {
      title: currentLanguageTexts.value.Todayvisitors,
      type: res.appTodayDataVO.num03,
    },
    {
      title: currentLanguageTexts.value.TodayRefund,
      type: res.appTodayDataVO.num04,
    },
  ];
};
</script>

<style lang="scss">
.headimg {
  width: 62rpx;
  height: 62rpx;
  margin-right: 20rpx;
  border-radius: 50%;
}

.title {
  font-size: 34rpx;
  font-weight: 600;
  color: #333333;
}

.yuer {
  position: relative;
  width: 700rpx;
  height: 340rpx;
  background-color: #fff;
  margin: 28rpx 0 0rpx;

  .bgcimg {
    width: 680rpx;
    height: 250rpx;
    border-radius: 10rpx;
  }

  .yuer_bom {
    height: 100rpx;
    padding: 20rpx 26rpx 0;
    box-sizing: border-box;

    image {
      width: 36rpx;
      height: 36rpx;
      margin-right: 16rpx;
    }

    font-size: 24rpx;
    color: #000000;
  }

  .one {
    font-size: 30rpx;
    color: #ffffff;
    position: absolute;
    top: 56rpx;
    left: 50rpx;
  }

  .two {
    font-size: 56rpx;
    font-weight: 600;
    color: #ffffff;
    position: absolute;
    top: 120rpx;
    left: 50rpx;
  }

  .three {
    font-size: 22rpx;
    color: #ffffff;
    position: absolute;
    top: 66rpx;
    right: 88rpx;
  }

  .four {
    width: 188rpx;
    height: 52rpx;
    background: #ffffff;
    border-radius: 26rpx;
    font-size: 22rpx;
    color: #00abff;
    line-height: 52rpx;
    text-align: center;
    position: absolute;
    top: 132rpx;
    right: 64rpx;
  }
}

.card {
  width: 700rpx;
  background: #ffffff;
  border-radius: 10rpx;
  padding: 10rpx 0rpx;
  box-sizing: border-box;
  margin: 40rpx 0;

  .card_item {
    position: relative;
    width: 140rpx;
    text-align: center;
    font-size: 24rpx;
    color: #777777;
    margin: 16rpx 0;

    image {
      width: 74rpx;
      height: 74rpx;
    }

    .type {
      font-size: 14rpx;
      line-height: 26rpx;
      text-align: center;
      position: absolute;
      right: 36rpx;
      top: 4rpx;
      z-index: 99;
    }
  }
}

.day_card {
  background-color: #fff;
  margin-bottom: 40rpx;

  .dayimg {
    text-align: right;
    width: 40rpx;
  }

  .title {
    padding: 18rpx 30rpx;
    box-sizing: border-box;
    border-bottom: 1rpx solid #ebebeb;

    .one {
      font-size: 34rpx;
      color: #000000;
    }

    .two {
      font-size: 24rpx;
      color: #777777;
      width: 350rpx;
      height: 34rpx;
    }

    image {
      width: 27rpx;
      height: 34rpx;
    }
  }
}

.grid {
  padding: 20rpx 80rpx;
  box-sizing: border-box;
  text-align: center;

  .grid_one {
    font-size: 26rpx;
    color: #777777;
    margin-bottom: 10rpx;
  }

  .grid_two {
    font-size: 34rpx;
    color: #000000;
  }
}
</style>
